package cn.pegasus.teamwork.interfaces.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.iyizhan.teamwork.base.service.impl.BaseServiceImpl;
import cn.pegasus.teamwork.interfaces.entity.BussInterfaceInfo;
import cn.pegasus.teamwork.interfaces.entity.BussInterfaceParam;
import cn.pegasus.teamwork.interfaces.mapper.BussInterfaceInfoMapper;
import cn.pegasus.teamwork.interfaces.mapper.BussInterfaceParamMapper;
import cn.pegasus.teamwork.interfaces.service.BussInterfaceInfoService;
/**
* @ClassName: BussInterfaceInfoServiceImpl 
* @Description:(接口实现层) 
* @author zhoulibo
* @date 2015年12月2日 上午11:46:37
 */
@Service
public class BussInterfaceInfoServiceImpl extends BaseServiceImpl<BussInterfaceInfo, BussInterfaceInfoMapper>  implements BussInterfaceInfoService{
	//注入当前dao对象
	@Autowired
	private BussInterfaceInfoMapper bussInterfaceInfoMapper;
	@Autowired
	private BussInterfaceParamMapper bussInterfaceParamMapper;
	public BussInterfaceInfoServiceImpl(){
		setMapperClass(BussInterfaceInfoMapper.class , BussInterfaceInfo.class);
	}

	/* (非 Javadoc)  
	* <p>Title: addInterfaceInfo</p>  
	* <p>Description:添加接口信息 </p>  
	* @param map
	* @param currentUserId
	* @return
	* @throws Exception  
	* @see cn.iyizhan.teamwork.interfaces.service.BussInterfaceInfoService#addInterfaceInfo(java.util.Map, java.lang.Long)
	* ASUS User
	* 2015年12月2日 
	*/
	public Map<String, Object> addInterfaceInfo(Map<String, Object> map,Long currentUserId) throws Exception {
		 Map<String,Object> resultMap= new HashMap<String,Object>();
	        try {
	        	BussInterfaceInfo interfaceInfo = M2O(map);
	            interfaceInfo.setCreateTime(new Date());
	            interfaceInfo.setCreateUserId(currentUserId);
	            bussInterfaceInfoMapper.insertByEntity(interfaceInfo);
	            resultMap.put("status", "success");
	        } catch (Exception e) {
	            resultMap.put("status", "failed");
	            resultMap.put("message", "添加接口信息报错！");
	            throw e;
	        }
	        return resultMap;
	    }
	
	/* (非 Javadoc)  
	* <p>Title: updateInterfaceInfo</p>  
	* <p>Description:修改接口 </p>  
	* @param map
	* @param currentUserId
	* @return
	* @throws Exception  
	* @see cn.iyizhan.teamwork.interfaces.service.BussInterfaceInfoService#updateInterfaceInfo(java.util.Map, java.lang.Long)
	* ASUS User
	* 2015年12月2日 
	*/
	public Map<String, Object> updateInterfaceInfo(Map<String, Object> map,Long currentUserId) throws Exception {
		  Map<String,Object> resultMap= new HashMap<String,Object>();
	        try {
	        	BussInterfaceInfo interfaceInfo = M2O(map);
	            interfaceInfo.setLastUpdateTime(new Date());
	            interfaceInfo.setLastUpdateUserId(currentUserId);
	            bussInterfaceInfoMapper.updateByEntity(interfaceInfo);
	            resultMap.put("status", "success");
	        } catch (Exception e) {
	            resultMap.put("status", "failed");
	            resultMap.put("message", "修改接口信息报错！");
	            throw e;
	        }
	        return resultMap;
	}
	/* (非 Javadoc)  
	* <p>Title: selectInterfaceInfo</p>  
	* <p>Description: 查询接口信息</p>  
	* @param map
	* @return
	* @throws Exception  
	* @see cn.iyizhan.teamwork.interfaces.service.BussInterfaceInfoService#selectInterfaceInfo(java.util.Map)
	* ASUS User
	* 2015年12月2日 
	*/
	public List<BussInterfaceInfo> selectInterfaceInfo(Map<String, Object> map) throws Exception {
		List<BussInterfaceInfo> list =new ArrayList<BussInterfaceInfo>();
		try{
			BussInterfaceInfo bussInterfaceInfo= new BussInterfaceInfo();
			bussInterfaceInfo.setAppId(Integer.valueOf(map.get("appId").toString()));
			list= bussInterfaceInfoMapper.selectByObject(bussInterfaceInfo);
		}catch(Exception e){
			throw e;
		}
		return list;
	}
	
	/* (非 Javadoc)  
	* <p>Title: deleteInterfaceInfo</p>  
	* <p>Description:删除接口 </p>  
	* @param map
	* @param currentUserId
	* @return
	* @throws Exception  
	* @see cn.iyizhan.teamwork.interfaces.service.BussInterfaceInfoService#deleteInterfaceInfo(java.util.Map, java.lang.Long)
	* ASUS User
	* 2015年12月2日 
	*/
	public Map<String, Object> deleteInterfaceInfo(Map<String, Object> map,Long currentUserId) throws Exception {
		 Map<String,Object> resultMap= new HashMap<String,Object>();
	        try {
	            int id = Integer.valueOf(map.get("id").toString());
	            BussInterfaceInfo interfaceInfo = new BussInterfaceInfo();
	            interfaceInfo.setId(id);
	            //删除接口信息
	            bussInterfaceInfoMapper.deleteByEntity(interfaceInfo);
	            BussInterfaceParam interfaceParam = new BussInterfaceParam();
	            interfaceParam.setInterfaceId(id);
	            //删除接口对应的参数信息
	            bussInterfaceParamMapper.deleteByEntity(interfaceParam);
	            resultMap.put("status", "success");
	        } catch (Exception e) {
	            resultMap.put("status", "failed");
	            resultMap.put("message", "删除接口信息报错！");
	            throw e;
	        }
	        return resultMap;
	}
	/* (非 Javadoc)  
	* <p>Title: getBussInterfaceInfo</p>  
	* <p>Description:获取某个接口信息 </p>  
	* @param id
	* @return
	* @throws Exception  
	* @see cn.iyizhan.teamwork.interfaces.service.BussInterfaceInfoService#getBussInterfaceInfo(java.lang.Integer)
	* ASUS User
	* 2015年12月2日 
	*/
	public BussInterfaceInfo getBussInterfaceInfo(Integer id) throws Exception {
		BussInterfaceInfo bussInterfaceInfo=new BussInterfaceInfo();
		bussInterfaceInfo.setId(id);
		bussInterfaceInfo=bussInterfaceInfoMapper.selectSingle(bussInterfaceInfo);
		return bussInterfaceInfo;
	}

	/**
	 * 通过接口id查询接口信息，接口参数信息
	 */
	public Map<String, Object> getBussInterfaceInfoAll(Integer id)
			throws Exception {
		Map<String, Object> maps = new HashMap<String,Object>();
		if(id > 0){
			BussInterfaceInfo bussInterfaceInfo=new BussInterfaceInfo();
			bussInterfaceInfo.setId(id);
			
			// 获取接口信息
			bussInterfaceInfo=bussInterfaceInfoMapper.selectSingle(bussInterfaceInfo);
			maps.put("info", bussInterfaceInfo);
			
			// 获取接口对应的参数列表
			BussInterfaceParam param = new BussInterfaceParam();
			param.setInterfaceId(id);
			List<BussInterfaceParam> lis  = bussInterfaceParamMapper.selectByObject(param);
			if(null != lis && lis.size() > 0){
				maps.put("params", lis);
			}
		}else{
			return null;
		}
		return maps;
	}
}
